import requests,json,io
from utils import sha256_hash
from config.config import NetConfig,AiConfig
from mq.producer import put_data
from aiutils import change_bgc
from filetransfer.ali_oss import upload_file2alioss

class LoginService():
    
    def verify_token(self,params):
        
        response = requests.post(NetConfig.ZERO_SYS_VERIFY_URL,params=params)
        
        data = json.loads(response.content)
        if data['code'] != 200:
            return False, data['data']
        else:
            return True , data['data']    
        
class PhotoBackgroundChangeService():
    
    def photo_bgc_change(self,photo_file,metadata):
        metadata["signature"] = sha256_hash(json.dumps(metadata))
        
        if AiConfig.ENABLE_LOCAL_PHOTO_AI:
            pil_image = change_bgc(photo_file,"com",metadata["params"]["new_bgc"]) 
            new_size = (int(metadata["params"]["photo_w"]),int(metadata["params"]["photo_h"]))
            pil_image = pil_image.resize(new_size)

            image_io = io.BytesIO()
            pil_image.save(image_io,format="png")
            image_io.seek(0)
            return image_io
        else:
            upload_file2alioss(photo_file)
            put_data(metadata=metadata)